<?php
class BbsController extends AppController {

	var $name = 'Bbs';
    var $helpers = array('Html','Javascript', 'Ajax');
    var $uses=array('Bb','Ctrinhbb','Ctrinh');
    var $components    = array('Cookie');

    //ghi chu

/**
*Phan chu de truoc khi chon them can cu cho cong trinh
*/
	function index1($idct) {
		$this->Cookie->write('idct', $idct);

		$this->Ctrinh->recursive=-1;
		$tenct=$this->Ctrinh->find('first',array('conditions'=>array('id'=>$idct),'fields'=>array('tenctrinh')));	

		$bb_daco=$this->Ctrinhbb->find('list',array('conditions'=>array('ctrinh_id'=>$idct)));
		$this->Bb->recursive = -1;
		$this->set('bbs', $this->paginate('Bb',array('id'=>$bb_daco)));
		$this->set(compact('idct','tenct'));

	}
/***
*Themcan cu moi-->admin
*/
	function index2() {
		$this->Bb->recursive = 0;
		$this->set('bbs', $this->paginate());
	}
/**
*Trang tim kiem ds can cu de them
*/
	function index() {
		$idct=$this->Cookie->read('idct');
		$this->Ctrinh->recursive=-1;
		$tenct=$this->Ctrinh->find('first',array('conditions'=>array('id'=>$idct),'fields'=>array('tenctrinh')));

		
	//	$this->set(compact('idct'));
		$bb_daco=$this->Ctrinhbb->find('list',array(
			'conditions'=>array('ctrinh_id'=>$idct),'fields'=>array('bb_id','bb_id')));
		$this->Bb->recursive = -1;
		$this->set('bbs', $this->paginate('Bb',array('id'=>$bb_daco)));

		$this->set(compact('tenct'));

	}
 	function timbb($q=null) {
               
      //  print_r($ztkbs);
	   $this->layout = 'ajax';
	   $this->Ctrinhbb->recursive=-1;
	   //loc ra nhung cai da co
	   $bb_daco=$this->Ctrinhbb->find('list',array('fields'=>array('bb_id','bb_id'),
	   		'conditions'=>array('ctrinh_id'=>$this->Cookie->read('idct'))));
	 // print_r($this->Cookie->read('idct'));
	   $this->Bb->recursive=-1;
       $bbs=$this->Bb->find('list',array(
       			'fields'=>array('id','tenbb'),
       			'conditions'=>array('AND'
       					=>array('Bb.tenbb like '=>'%'.$q.'%'),
       					'NOT'=>array('id'=>$bb_daco))));
    //   print_r($bbs);
 //print_r($bb_daco);
       $this->set(compact('q','bbs'));
    }
    function thembb(){
    	if(empty($this->data)){
        	$this->Session->setFlash(__('Anh (chị) chọn chọn căn cứ !!', true));
			$this->redirect(array('action' => 'index'));
        }
    	foreach ($this->data['Bb']['id'] as $k=>$value) {
    		$this->Ctrinhbb->create();
    		$record['Ctrinhbb'] ['bb_id']=$value;
			$record['Ctrinhbb'] ['ctrinh_id']=$this->Cookie->read('idct');

			if ($this->Ctrinhbb->save($record)) {
				$this->Session->setFlash(__('Căn cứ đã lưu, Anh (chị) có thể tiếp tục chọn căn cứ', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The bb could not be saved. Please, try again.', true));
			}
		}
    	
    }
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid bb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('bb', $this->Bb->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Bb->create();
			if ($this->Bb->save($this->data)) {
				$this->Session->setFlash(__('The bb has been saved', true));
				$this->redirect(array('action' => 'index2'));
			} else {
				$this->Session->setFlash(__('The bb could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid bb', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Bb->save($this->data)) {
				$this->Session->setFlash(__('The bb has been saved', true));
				$this->redirect(array('action' => 'index2'));
			} else {
				$this->Session->setFlash(__('Dữ liệu được lưu, vui lòng thử lại', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Bb->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for bb', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Bb->query('delete from ctrinhbbs where ctrinh_id='.$this->Cookie->read('idct').' and bb_id='.$id)) {
			$this->Session->setFlash(__('Căn cứ đã được xóa', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Bb was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
